package net.ojbk.study.twobox.service;

import net.ojbk.study.twobox.beans.po.Job;
import net.ojbk.study.twobox.beans.po.Student;
import net.ojbk.study.twobox.dao.ClassMapper;
import net.ojbk.study.twobox.dao.JobMapper;
import net.ojbk.study.twobox.dao.ReferMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/4/16
 * Time: 11:42
 * Description: No Description
 */
@Service
public class StudentService extends SupperService<Student> {
    @Autowired
    ReferMapper referMapper;

    @Autowired
    ClassMapper classMapper;

    @Autowired
    JobMapper jobMapper;

    public boolean isGoodTime(Date time1, Date time2) {

        LocalDateTime now_time1 = LocalDateTime.ofInstant(time1.toInstant(), ZoneId.systemDefault());
        LocalDateTime now_time2 = LocalDateTime.ofInstant(time2.toInstant(), ZoneId.systemDefault());

        long xcdays = now_time2.toLocalDate().toEpochDay() - now_time1.toLocalDate().toEpochDay();

        if (xcdays >= 7 && xcdays <= 120)
            return true;
        return false;
    }

    public void check(Student o) throws Exception{
        //咨询外键referid必须在咨询表中存在
        if (referMapper.selectByPrimaryKey(o.getReferid()) == null)
            throw new Exception("此同学未咨询过，质询表不存在。");
        // 班级classid可以先不填,如果不为空则必须在班级表中有该主键id
        if (o.getClassid() != null && classMapper.selectByPrimaryKey(o.getClassid()) == null)
            throw new Exception("输入班级不为空，但是此班级不存在。");
        // 毕业时间jobTime要大于入学时间beginTime,并且要在一定范围内(7天到120天范围)
        if (!isGoodTime(o.getBegintime(), o.getJobtime()))
            throw new Exception("毕业时间jobTime要大于入学时间beginTime,并且要在一定范围内(7天到120天范围)");

    }

    /**
     * 增
     */
    @Override
    public void addOne(Student o, BindingResult br) throws Exception {
        this.validated(br, Student.class);
        // 学号stuCode通过自定义规则生成唯一的编号
        o.setStucode(UUID.randomUUID().toString());
        check(o);
        this.dao.insert(o);
    }

    /**
     * 删
     */
    @Override
    public void deleteOne(int id) throws Exception {
        Student student = this.dao.selectByPrimaryKey(id);
        if (student == null)
            throw new Exception("该学生不存在。");
        Job job = new Job();
        job.setStucode(student.getStucode());
        if (jobMapper.selectCount(job) > 0)
            throw new Exception("检查crm_job就业表中是否有该条学生的学号,如果没有该id外键");
        this.dao.deleteByPrimaryKey(id);
    }

    /**
     * 改
     */
    @Override
    public void updateOne(Student o, BindingResult br) throws Exception {
        this.validated(br, Student.class);
        Student student = this.dao.selectByPrimaryKey(o.getId());
        o.setReferid(student.getReferid());
        check(o);
        this.dao.updateByPrimaryKeySelective(o);
    }
}
